﻿using Newtonsoft.Json;
using ProductionManage.Models.DataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using wangyaxin.Jiangzhenying.Workstations;
using wangyaxin.Jiangzhenying.Workstations.Dto;
using static Volo.Abp.Identity.Settings.IdentitySettingNames;
using wangyaxin.Wang.Dto;
using Microsoft.AspNetCore.Mvc;
using test.demo.Model.DV;
using wangyaxin.Zhangduo.WorkShops.Dto;
using test.demo.Model.PRO生产管理;

namespace wangyaxin.Jiangzhenying
{
    [ApiExplorerSettings(GroupName = "江珍莹")]
    public class WorkstationService : ApplicationService, IWorkstationIService
    {
        private readonly IRepository<Workstation, int> _repository;
        private readonly IRepository<WorkShop, int> _workShoprepository;
        private readonly IRepository<Pro_Process, int> _proProcesspository;
        public WorkstationService(IRepository<Workstation, int> repository, IRepository<WorkShop, int> workShoprepository, IRepository<Pro_Process, int> proProcesspository)
        {
            _repository = repository;
            _workShoprepository = workShoprepository;
            _proProcesspository = proProcesspository;
        }

        /// <summary>
        /// 获取工位列表
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetWorkstationLists(string? workstationCode, string? workstationName, int workShopId, int processId)
        {
            var list = await _repository.GetListAsync();
            if(!string.IsNullOrEmpty(workstationCode))
            {
                list = list.Where(x => x.WorkstationCode.Contains(workstationCode)).ToList();
            }
            if (!string.IsNullOrEmpty(workstationName))
            {
                list = list.Where(x => x.WorkstationName.Contains(workstationName)).ToList();
            }
            if (workShopId!= 0)
            {
                list = list.Where(x => x.WorkShopId == workShopId).ToList();
            }
            if (processId!= 0)
            {
                list = list.Where(x => x.ProcessId == processId).ToList();
            }
            var workstation = ObjectMapper.Map<List<Workstation>, List<WorkstationDto>>(list);
            return JsonConvert.SerializeObject(new PageDto<WorkstationDto> { code = "", data = workstation, msg = "", count = workstation.Count });
        }

        /// <summary>
        /// 新增工作站
        /// </summary>
        /// <returns></returns>
        public async Task<int> AddWorkstation(Workstation input)
        {
            var workstation = ObjectMapper.Map<Workstation, Workstation>(input);
            var src = await _workShoprepository.GetAsync(x => x.WorkShopCode == input.WorkShopCode);
            input.WorkShopId = src.Id;
            input.WorkShopCode = src.WorkShopCode;
            input.WorkShopName = src.WorkShopName;
            var src2 = await _proProcesspository.GetAsync(x => x.ProcessCode == input.ProcessCode);
            input.ProcessId = src2.Id;
            input.ProcessCode = src2.ProcessCode;
            input.ProcessName = src2.ProcessName;
            await _repository.InsertAsync(input);
            return 1;
        }

        /// <summary>
        /// 删除工作站
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<string> DeleteWorkstation(int id)
        {
            var workstation=await _repository.GetAsync(x=>x.Id==id);
            await _repository.DeleteAsync(workstation,true);
            return JsonConvert.SerializeObject(new PageDto<WorkstationDto> { code = "200" ,data=null  , msg = "删除成功", count = 1 });
        }

        /// <summary>
        /// 绑定动态车间
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetWorkShop()
        {
            var workShop = await _workShoprepository.GetListAsync();
            var workShopDto = ObjectMapper.Map<List<WorkShop>, List<WorkShop>>(workShop);
            return JsonConvert.SerializeObject(new PageDto<WorkShop> { code = "", data = workShopDto, msg = "成功", count = workShopDto.Count });
        }

        /// <summary>
        /// 绑定动态工序
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetProProcess()
        {
            var proProcess = await _proProcesspository.GetListAsync();
            var proProcessDto = ObjectMapper.Map<List<Pro_Process>, List<Pro_Process>>(proProcess);
            return JsonConvert.SerializeObject(new PageDto<Pro_Process> { code = "", data = proProcessDto, msg = "成功", count = proProcessDto.Count });
        }
    }
}
